int LCA( Tree T, int u, int v ){
	Tree bt=T,p1=T,p2=T,q1,q2;
	while(1){
		if(bt==NULL) break;
		if(bt->Key==u) break;
		p1=bt;
		if(bt->Key>u) bt=bt->Left;
		else bt=bt->Right;
	}
	if(bt==NULL) {
		return ERROR;
	}
	q1=bt;
	bt=T;
	while(1){
		if(bt==NULL) break;
		if(bt->Key==v) break;
		p2=bt;
		if(bt->Key>v) bt=bt->Left;
		else bt=bt->Right;
	}
	if(bt==NULL) return ERROR;
	q2=bt;
	if(u>v){
		if(u>T->Key&&v<T->Key) return T->Key;
	}
	else {
		if(v>T->Key&&u<T->Key) return T->Key;
	}
	if(u==v) return q1->Key;
	Tree pre1=q1;
	while(1){
		if(pre1==NULL) break;
		if(pre1->Key==v) break;
		if(pre1->Key>v) pre1=pre1->Left;
		else pre1=pre1->Right;
	}
	if(pre1!=NULL){
		return q1->Key;
	}
	Tree pre2=q2;
	while(1){
		if(pre2==NULL) break;
		if(pre2->Key==u) break;
		if(pre2->Key>u) pre2=pre2->Left;
		else pre2=pre2->Right;
	}
	if(pre2!=NULL) return q2->Key;
	if(p2==p1) return p1->Key;
	if(pre1==NULL&&pre2==NULL){
		Tree s;
		int max,min;
		if(u<T->Key){
			s=T->Left;
			if(u>v) {
				max=u;
				min=v;
			}
			else {
				max=v;
				min=u;
			}
			while(1){
				if(s->Key<=max&&s->Key>min){
					return s->Key;
				} 
				else if(s->Key>max&&s->Key>min){
					s=s->Left;
				}
				else if(s->Key<max&&s->Key<min){
					s=s->Right;
				}
			}
		}
		else if(u>T->Key){
			s=T->Right;
			if(u>v) {
				max=u;
				min=v;
			}
			else {
				min=u;
				max=v;
			}
			while(1){
				if(s->Key>=min&&s->Key<max) return s->Key;
				else if(s->Key<min&&s->Key<max){
					s=s->Right;
				}
				else if(s->Key>min&&s->Key>min){
					s=s->Left;
				}
			}
		}
	}
}
